#  r==>right  l==>left

str = 'abcdefghijklmnopqa'

# 使用内置函数len获取str长度
print(len(str))

# 查找：find/index/rfind/rindex

# S.find(sub[, start[, end]]) -> int
# Return -1 on failure.
print(str.find('c'))  # 2
print(str.find('c', 3, 4))
# S.index(sub[, start[, end]]) -> int
# Raises ValueError when the substring is not found.
print(str.index('c'))  # 2
# difference:
print(str.find('z'))  # -1
# print(str.index('z'))  # ValueError: substring not found

# find 与 rfind的区别
"""
        S.find(sub[, start[, end]]) -> int

        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.

        Return -1 on failure.
        """
"""
        S.rfind(sub[, start[, end]]) -> int

        Return the highest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.

        Return -1 on failure.
        """
print(str.find('a'))
print(str.rfind('a'))

print('--------------------------')

# 判断算法
# startswith, endswith, isalpha, isdigit, isalnum, isspace
print(str.startswith('a'))
print(str.startswith('c'))
print('asd'.isalpha())  # True
print('asd23asd'.isalnum())  # True
print('   '.isspace())  # True
print('2.1'.isdigit())  # False
print('917'.isdigit())  # False

print('--------------------------')

# replace 算法
#  def replace(self, *args, **kwargs):
"""
        Return a copy with all occurrences of substring old replaced by new.

          count
            Maximum number of occurrences to replace.
            -1 (the default value) means replace all occurrences.

        If the optional argument count is given, only the first count occurrences are
        replaced.
        """
str = 'asdasdasdasdasdasdadsad'
strr = str.replace('a', 'A')
strrr = str.replace('a', 'A', 2)  # 只替换了前两次出现的'a'
# st = str('a','A', 20)  # TypeError: 'str' object is not callable
print(str)
print(strr)
print(strrr)

print('--------------------')

# 切换大小写 capitalize, lower, upper, title
# capitalize: make the first character have upper case and the rest lower
#         case.
print('Hello.asd,asda.qr'.capitalize())
# lower: Return a copy of the string converted to lowercase
print('AxFnmmsAKSJDNLC'.lower())
# upper:
print('AxFnmmsAKSJDNLC'.upper())
# title: words start with uppercased characters and all remaining
#         cased characters have lower case.
print('AxFn,mms,AKSJ.DNLC'.title())

print('------------------')

#
# ljust
# Return a left-justified string of length width.
# Padding is done using the specified fill character (default is a space).
print('monkey'.ljust(7), 'monkey')
print('monkey'.ljust(10, '-'), 'monkey')
# rjust
print('monkey'.rjust(7), 'monkey')
print('monkey'.rjust(10, '-'), 'monkey')
# lstrip rstrip strip
# lstrip
# Return a copy of the string with leading whitespace removed.
#         If chars is given and not None, remove characters in chars instead.
print('       monkey     '.lstrip(), 'monkey')
print('       monkey     '.rstrip(), 'monkey')
print('       monkey     '.strip(), 'monkey')
print('+++++++monkey     '.lstrip('+'), 'monkey')
